package com.example.csmall.product.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

/**
 * Spring data redis 配置类信息
 */
@Slf4j
@Configuration
public class RedisConfiguration {

    //配置类加载时调用此方法证明配置类调用成功
    public RedisConfiguration() {
        log.debug("创建配置类: RedisConfiguration");
    }

    @Bean
    public RedisTemplate<String , Serializable> RedisTemplate(
            RedisConnectionFactory rcf
    ){
        RedisTemplate<String ,Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(rcf);  //设置redis的连接工厂
        redisTemplate.setKeySerializer(RedisSerializer.string()); //设置key的序列化格式
        redisTemplate.setValueSerializer(RedisSerializer.json()); //设置value的序列化格式
        return redisTemplate;
    }

}
